package org.BUPT.api.controller;


import org.BUPT.service.UserAndSubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.UUID;

@CrossOrigin
@RestController
public class AuthServiceController {

    public static String ADMIN   ="1";
    public static String TEACHER ="2";
    public static String STUDENT ="3";

    @Autowired
    UserAndSubjectService userAndSubjectService;

    @PostMapping("/login")
    public void login(
            HttpServletResponse response,
            @RequestParam("userId") int userId,
            @RequestParam("password") String password
    ){

        UUID uuid = UUID.randomUUID();
        String token = userId+"_"+uuid.toString().replace('_','-');
        Cookie cookie = new Cookie("token",token);
        response.addCookie(cookie);

    }

    @GetMapping("/logout")
    public String logout(
            @CookieValue("token") String token
    ){
        System.out.println(token);
        int userId = Integer.parseInt(token.split("_")[0]);
        return ""+userId;
    }



    /**
     * 是否可以删除问题
     * 提问者
     * @param request
     * @return
     */
    public int canIDeleteQuestion(HttpServletRequest request,String questionOwnerId){
        if(questionOwnerId==null||questionOwnerId.equals("")) return -1;
        String userId=getUserId(request);
        if(userId==null) return -1;
        if(userId.equals(questionOwnerId)){
            return Integer.parseInt(userId);
        };
        return -1;
    }

    /**
     * 是否可以修改问题
     * 提问者，老师
     * @param request
     * @return
     */
    public int canIModeifyQuestion(HttpServletRequest request,String questionOwnerId){
        if(questionOwnerId==null||questionOwnerId.equals("")) return -1;
        String userId=getUserId(request);
        if(userId==null) return -1;
        String roleId=userAndSubjectService.getUserRole(userId);
        if(roleId.equals(TEACHER) || userId.equals(questionOwnerId)) return Integer.parseInt(userId);
        return -1;
    }

    /**
     * 是否可以提出问题
     * 学生
     * @param request
     * @return
     */
    public int canIAskQuestion(HttpServletRequest request){
        String userId=getUserId(request);
        if(userId==null) return -1;
        String roleId=userAndSubjectService.getUserRole(userId);
        if(roleId.equals(STUDENT)) return Integer.parseInt(userId);
        return -1;
    }

    /**
     * 是否可以添加问题关联的知识点
     * 提问者，老师
     * @param request
     * @return
     */
    public int canIAddKnowledgePoint(HttpServletRequest request,String questionOwnerId){
        return canIModeifyQuestion(request, questionOwnerId);
    }

    /**
     * 是否可以删除问题关联的知识点
     * 提问者，老师
     * @param request
     * @return
     */
    public int canIDeleteKnowledgePoint(HttpServletRequest request,String questionOwnerId){
        return canIModeifyQuestion(request, questionOwnerId);
    }

    /**
     * 是否可以修改问题关联的知识点
     * 提问者，老师
     * @param request
     * @return
     */
    public int canIModifyKnowledgePoint(HttpServletRequest request,String questionOwnerId){
        return canIModeifyQuestion(request, questionOwnerId);
    }

    /**
     * 是否可以回答问题
     * 老师
     * @param request
     * @return
     */
    public int  canIAnswerQuestion(HttpServletRequest request){
        String userId=getUserId(request);
        if(userId==null) return -1;
        String roleId=userAndSubjectService.getUserRole(userId);
        if(roleId.equals(TEACHER)) return Integer.parseInt(userId);
        return -1;
    }

    /**
     * 是否可以修改回答
     * 回答者（老师）
     * @param request
     * @return
     */
    public int canIModifyMyAnswer(HttpServletRequest request,String answerOwnerId){
        if(answerOwnerId==null||answerOwnerId.equals("")) return -1;
        String userId=getUserId(request);
        if(userId==null) return -1;
        String roleId=userAndSubjectService.getUserRole(userId);
        if(roleId.equals(TEACHER)&&answerOwnerId.equals(userId)){
            return Integer.parseInt(userId);
        }
        return -1;
    }

    /**
     * 是否可以删除回答
     * 回答者（老师）
     * @param request
     * @return
     */
    public int canIDeleteMyAnswer(HttpServletRequest request,String answerOwnerId){
        return canIModifyMyAnswer(request, answerOwnerId);
    }

    /**
     * 是否可以修改自己的评论
     * 评论者
     * @param request
     * @return
     */
    public int canIModifyMyComment(HttpServletRequest request,String commentOwnerId){
        if(commentOwnerId==null||commentOwnerId.equals("")) return -1;
        String userId=getUserId(request);
        if(commentOwnerId.equals(userId)){
            return Integer.parseInt(userId);
        }
        return -1;
    }

    /**
     * 是否可以删除自己的评论
     * 评论者
     * @param request
     * @return
     */
    public int canIDeleteMyComment(HttpServletRequest request,String commentOwnerId){
        return canIModifyMyComment(request,commentOwnerId);
    }

    /**
     * 是否可以查看自己绑定的所有课程
     * 自己
     * @param request
     * @return
     */
    public boolean canIViewMyAllSubject(HttpServletRequest request){
        return false;
    }

    /**
     * 是否可以查看查看某个课程下的老师
     * 所有人
     * @param request
     * @return
     */
    public boolean canIViewViewAllTeachersOfSubject(HttpServletRequest request){
        return false;
    }


    //---------------------以下私有工具方法----------------------------


    protected String getUserId(HttpServletRequest request){
        Cookie[] cookies=request.getCookies();
        for(Cookie cookie:cookies){
            if(cookie.getName().equals("token")){
                return cookie.getValue().split("_")[0];
            }
        }
        return null;
    }

//    @PostMapping("/page/{pageId}")
//    public String getPage(
////            @RequestParam String token,
//            @PathVariable int pageId
//    ){
//
//    }
//
//
//    @PostMapping("/allSubject")
//    public String getAllSubject(
////            @RequestParam("token") String token
//    ){
//
//    }
//
//
//
//    @PostMapping("/question/{questionId}")
//    String getQuestion(
//            @PathVariable("questionId") int questionId
//    ){
//
//    }
//
//
//    @PostMapping("/getSubscribedQuestion")
//    String getSubscribedQuestionList(
////            @RequestParam("token") String token
//    ){
//
//    }
//
//
//    @GetMapping("/getAskedQuestion")
//    String getAskedQuestionList(
////            @RequestParam("token") String token
//    ){
//
//    }
//
//
//    @PostMapping("/addQuestion")
//    public String addQuestion(
////            @RequestParam(value = "token") String token,
//            @RequestParam(value = "title") String title,
//            @RequestParam(value = "content") String content,
//            @RequestParam(value = "subject") String subject,
//            @RequestParam(value = "tags") String[] tags,
//            @RequestParam(value = "teacherId") int teacherId
//    ){
//
//    }
//
//
//    @PostMapping("/modifyQuestion")
//    public String modifyQuestion(
////            @RequestParam(value = "token") String token,
//            @RequestParam(value = "questionId") int id,
//            @RequestParam(value = "title") String title,
//            @RequestParam(value = "content") String content,
//            @RequestParam(value = "subject") String subject,
//            @RequestParam(value = "tags") String[] tags
//    ){
//
//    }
//
//    @PostMapping("/addAnswer")
//    public String addAnswer(
////            @RequestParam(value = "token") String token,
//            @RequestParam(value = "questionId") int questionId,
//            @RequestParam(value = "content") String content
//    ){
//
//    }
//
//
//    @PostMapping("/modifyAnswer")
//    public String modifyAnswer(
//            @RequestParam(value="answerId") int answerId,
//            @RequestParam(value = "content") String content
//    ){
//
//
//    }
//
//
//    @PostMapping("/addComment")
//    public String addComment(
////            @RequestParam(value = "token") String token,
//            @RequestParam(value = "answerId") int answerId,
//            @RequestParam(value = "parentId") int parentId,
//            @RequestParam(value = "content") String content
//    ){
//
//    }
//
//
//    @PostMapping("/modifyComment")
//    public String modifyComment(
//
//            @RequestParam(value = "commentId") int commentId,
//            @RequestParam(value = "content") String content
//    ){
//
//    }
//
//    @PostMapping("/subscribeQuestion")
//    public String subscribeQuestion(
////            @RequestParam(value = "token") String token,
//            @RequestParam(value = "questionId") int questionId
//    ){
//
//    }
//
//    @PostMapping("/unsubscribeQuestion")
//    public String unsubscribeQuestion(
////            @RequestParam(value = "token") String token,
//            @RequestParam(value = "questionId") int questionId
//    ){
//
//    }
//
//
//    @PostMapping("/likeAnswer")
//    public String likeAnswer(
////            @RequestParam(value = "token") String token,
//            @RequestParam(value = "answerId") int answerId
//    ){
//
//    }
//
//
//    @PostMapping("/answerIsLikedByUser")
//    public int getAnswerScoreByUser(
////            @RequestParam(value = "token") String token,
//            @RequestParam(value = "answerId") int answerId
//    ){
//
//    }
//
//
//    @PostMapping("/getLikeCount")
//    public int getAnswerScoreByUser(
////            @RequestParam(value = "token")
//            @RequestParam(value = "answerId") int answerId
//    ){
//
//    }
//
//
//
//    @GetMapping("/getMessage/{userId}")
//    public String getMessage(@PathVariable int userId){
//
//
//    }
////
//
//
//    @GetMapping("/Message/{messageId}")
//    public void visitMessage(@PathVariable int messageId){
//
//    }








}
